package services

import (
	"bsm/logger"
	"bsm/utils"
)

type ConvertService interface {
	ConvertExcelToPdfOle(xlsxfile string) (string, error)
	ConvertExcelToPdfLibre(xlsxfile string) (string, error)
	ConvertExcelToPdf(xlsxfile string) (string, error)
}

type convertServiceImpl struct {
	// converterOLE   *utils.ExcelPdfConverterOLE
	converterLibre *utils.ExcelPdfConverterLibre
}

func NewConvertService() ConvertService {
	// excelPdfConverterOLE, err := utils.NewExcelPdfConverterOLE()
	// if err != nil {
	// 	   logger.Fatalf("创建OLE转换器失败: %v", err)
	// }
	excelPdfConverterLibre, err := utils.NewExcelPdfConverterLibre(3)
	if err != nil {
		logger.Errorf("创建LibreOffice转换器失败: %v", err)
	}

	return &convertServiceImpl{
		// converterOLE:   excelPdfConverterOLE,
		converterLibre: excelPdfConverterLibre,
	}
}

func (e *convertServiceImpl) ConvertExcelToPdfOle(xlsxfile string) (string, error) {
	converter := utils.NewExcelPdfConverterOLE(xlsxfile)
	pdfFile, err := converter.Convert()

	if err != nil {
		logger.Errorf("OLE转换失败: %v", err)
	}
	return pdfFile, err
}

func (e *convertServiceImpl) ConvertExcelToPdfLibre(xlsxfile string) (string, error) {

	pdfFile, err := e.converterLibre.ConvertExcelToPDF(xlsxfile)
	if err != nil {
		logger.Errorf("LibreOffice转换失败: %v", err)
	}

	return pdfFile, err
}

func (e *convertServiceImpl) ConvertExcelToPdf(xlsxfile string) (string, error) {
	return e.ConvertExcelToPdfLibre(xlsxfile)
}
